package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Response;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description = "讲师管理")
public class EduTeacherController {

    @Resource
    private EduTeacherService teacherService;

    // 查询教师表的所有数据
    @ApiOperation("所有讲师列表")
    @GetMapping()
    private Response findAllTeacher() {
        List<EduTeacher> eduTeachers = teacherService.list(null);
        return Response.SUCCESS().data("items", eduTeachers);
    }

    // 讲师逻辑删除
    @ApiOperation("根据ID删除讲师")
    @DeleteMapping("/{id}")
    private Response removeTeachder(
            @PathVariable
            @ApiParam(name = "id", value = "讲师ID", required = true) String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return Response.SUCCESS();
        }
        return Response.Failed();
    }

    // 分页查询讲师
    @ApiOperation("分页查询讲师")
    @GetMapping("/{current}/{limit}")
    private Response pageListTeacher(@PathVariable Long current,
                                     @PathVariable Long limit) {
        if (limit > 40) {
            limit = 40L;
        }
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        teacherService.page(eduTeacherPage, null);
        Long total = eduTeacherPage.getTotal(); // 总记录数
        List<EduTeacher> records = eduTeacherPage.getRecords(); // 每页数据
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return Response.SUCCESS().data(map);
    }

    // 条件查询带分页功能
    @ApiOperation("条件查询带分页功能")
    @PostMapping("/{current}/{limit}")
    private Response pageTeacherCondition(@PathVariable Long current,
                                          @PathVariable Long limit,
                                          @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        wrapper.orderByDesc("gmt_create");
        teacherService.page(eduTeacherPage, wrapper);
        Long total = eduTeacherPage.getTotal(); // 总记录数
        List<EduTeacher> records = eduTeacherPage.getRecords(); // 每页数据
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return Response.SUCCESS().data(map);
    }

    // 添加讲师方法
    @ApiOperation("添加讲师方法")
    @PostMapping
    private Response addTeacher(@RequestBody EduTeacher eduTeacher) {
        System.out.println(eduTeacher);
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return Response.SUCCESS();
        }
        return Response.Failed();
    }

    // 根据讲师ID查询
    @ApiOperation("根据讲师ID查询")
    @GetMapping("/{id}")
    private Response getTeacher(@PathVariable Long id) {
        EduTeacher teacher = teacherService.getById(id);
        return Response.SUCCESS().data("rows", teacher);
    }

    // 讲师修改功能
    @ApiOperation("讲师修改功能")
    @PutMapping()
    private Response updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return Response.SUCCESS();
        }
        return Response.Failed();
    }

}
